package com.itlangzi.springboot.mybatis;

import com.itlangzi.mybatis.factory.PaginationFactory;
import com.itlangzi.mybatis.factory.PaginationWrapperFactory;
import com.itlangzi.mybatis.interceptors.PaginationInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;

import java.util.Properties;


@org.springframework.context.annotation.Configuration
@MapperScan("com.itlangzi.**.mapper")
public class MyBatisConfiguration implements ConfigurationCustomizer {

	@Override
	public void customize(Configuration configuration) {
		// 是否开启自动驼峰命名规则（camel case）映射，即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn
		// 的类似映射。默认false
		configuration.setMapUnderscoreToCamelCase(true);
		configuration.setLogPrefix("MyBatis-");

		// 设置分页工厂
		configuration.addInterceptor(registryPaginationInterceptor());

		configuration.setCacheEnabled(true);

		// 设置自定义的分页包装类工厂
		configuration.setObjectWrapperFactory(new PaginationWrapperFactory());
		// 设置分页工厂
		configuration.setObjectFactory(new PaginationFactory());


	}

	public Interceptor registryPaginationInterceptor() {
		PaginationInterceptor interceptor = new PaginationInterceptor();
		Properties properties = new Properties();
		interceptor.setProperties(properties);
		return interceptor;
	}

}
